package jianda;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.HTMLWriter;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class Jianda4 {
    public static void main(String[] args) throws IOException {
        Document doc = null;
        try{
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/jianda/Jianda1.xml");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 修改节点属性
        Element root = doc.getRootElement();
        List student = root.elements("student");
        for (Iterator it = student.listIterator(); it.hasNext();) {
            Element element = (Element) it.next();
            System.out.println("id为:"+element.attributeValue("id"));
            for (Iterator itscore = element.elementIterator();itscore.hasNext();){
                Element score = (Element) itscore.next();
                System.out.println("姓名为:"+score.attributeValue("name"));
            }
        }
        XMLWriter writer = new HTMLWriter(new FileWriter("src/jianda/Jianda1.xml"));
        writer.write(doc);
        writer.close();
    }
}
